package advertising.handler

import org.apache.commons.lang3.StringUtils
import org.apache.spark.sql.Row

import scala.collection.mutable.ArrayBuffer

//  .--,       .--,
// ( (  \.---./  ) )
//  '.__/o   o\__.'
//     {=  ^  =}
//      >  -  <
//     /       \
//    //       \\
//   //|   .   |\\
//   "'\       /'"_.-~^`'-.
//      \  _  /--'         `
//    ___)( )(___
//   (((__) (__)))    高山仰止, 景行行止; 虽不能至, 心向往之.
//                    High mountains, scenery. Although not to, the heart yearns for it.

/**
  * 获取原始数据中的值
  */
object FieldValueHandler {

  def getCallbackDate(row: Row) = {
    row.getAs[String]("callbackDate")
  }

  def getLatitude(row: Row) = {
    row.getAs[Double]("latitude")
  }

  def getLongitude(row: Row) = {
    row.getAs[Double]("longitude")
  }

  def getKeywords(row: Row) = {
    row.getAs[String]("keywords")
  }

  def getAppName(row: Row) = {
    val appId = getAppId(row)
    val jedis = JedisHandler.getJedis
    val appName = jedis.hget("appname", appId)
    jedis.close()
    appName
  }

  def getOSName(row: Row) = {
    parseClientName(row.getAs[Int]("client"))
  }

  def getDeviceName(row: Row) = {
    parseDeviceName(row.getAs[Int]("deviceType"))
  }

  def getNetworkName(row: Row) = {
    row.getAs[String]("networkMannerName")
  }

  def getIspName(row: Row) = {
    row.getAs[String]("ispName")
  }

  def getCityName(row: Row) = {
    row.getAs[String]("cityName")
  }

  def getProvinceName(row: Row) = {
    row.getAs[String]("provinceName")
  }

  def getAdPlatformProviderId(row: Row) = {
    row.getAs[Int]("adPlatformProviderId")
  }

  def getAppId(row: Row) = {
    row.getAs[String]("appId")
  }

  def getAdSpaceType(row: Row) = {
    try {
      val adSpaceType = row.getAs[Int]("adSpaceType")
      if (adSpaceType < 10) {
        "LC0" + adSpaceType
      } else {
        "LC" + adSpaceType
      }
    } catch {
      case e: Exception => "未知"
    }
  }

  def getRequestMode(row: Row, default: Int): Int = {
    try {
      row.getAs[Int]("requestMode")
    } catch {
      case e: Exception => default
    }
  }

  def getProcessNode(row: Row, default: Int): Int = {
    try {
      row.getAs[Int]("processNode")
    } catch {
      case e: Exception => default
    }
  }

  def getIsEffective(row: Row, default: Int): Int = {
    try {
      row.getAs[Int]("isEffective")
    } catch {
      case e: Exception => default
    }
  }

  def getIsBilling(row: Row, default: Int): Int = {
    try {
      row.getAs[Int]("isBilling")
    } catch {
      case e: Exception => default
    }
  }

  def getIsBid(row: Row, default: Int): Int = {
    try {
      row.getAs[Int]("isBid")
    } catch {
      case e: Exception => default
    }
  }

  def getAdOrderId(row: Row, default: Int): Int = {
    try {
      row.getAs[Int]("adOrderId")
    } catch {
      case e: Exception => default
    }
  }

  def getIsWin(row: Row, default: Int): Int = {
    try {
      row.getAs[Int]("isWin")
    } catch {
      case e: Exception => default
    }
  }

  def getWinPrice(row: Row, default: Double): Double = {
    try {
      row.getAs[Double]("isWin")
    } catch {
      case e: Exception => default
    }
  }

  def getAdPayment(row: Row, default: Double): Double = {
    try {
      row.getAs[Double]("adPayment")
    } catch {
      case e: Exception => default
    }
  }

  def getAllDriverId(row: Row) = {
    val driversId = ArrayBuffer[String]()
    val imei = row.getAs[String]("imei")
    if (!StringUtils.isBlank(imei)) {
      driversId += "imei:" + imei
    }
    val mac = row.getAs[String]("mac")
    if (!StringUtils.isBlank(mac)) {
      driversId += "mac:" + mac
    }
    val idfa = row.getAs[String]("idfa")
    if (!StringUtils.isBlank(idfa)) {
      driversId += "idfa:" + idfa
    }
    val openudid = row.getAs[String]("openudid")
    if (!StringUtils.isBlank(openudid)) {
      driversId += "openudid:" + openudid
    }
    val androidid = row.getAs[String]("androidid")
    if (!StringUtils.isBlank(androidid)) {
      driversId += "androidid:" + androidid
    }

    val imeimd5 = row.getAs[String]("imeimd5")
    if (!StringUtils.isBlank(imeimd5)) {
      driversId += "imeimd5:" + imeimd5
    }
    val macmd5 = row.getAs[String]("macmd5")
    if (!StringUtils.isBlank(macmd5)) {
      driversId += "macmd5:" + macmd5
    }
    val idfamd5 = row.getAs[String]("idfamd5")
    if (!StringUtils.isBlank(idfamd5)) {
      driversId += "idfamd5:" + idfamd5
    }
    val openudidmd5 = row.getAs[String]("openudidmd5")
    if (!StringUtils.isBlank(openudidmd5)) {
      driversId += "openudidmd5:" + openudidmd5
    }
    val androididmd5 = row.getAs[String]("androididmd5")
    if (!StringUtils.isBlank(androididmd5)) {
      driversId += "androididmd5:" + androididmd5
    }

    val imeisha1 = row.getAs[String]("imeisha1")
    if (!StringUtils.isBlank(imeisha1)) {
      driversId += "imeisha1:" + imeisha1
    }
    val macsha1 = row.getAs[String]("macsha1")
    if (!StringUtils.isBlank(macsha1)) {
      driversId += "macsha1:" + macsha1
    }
    val idfasha1 = row.getAs[String]("idfasha1")
    if (!StringUtils.isBlank(idfasha1)) {
      driversId += "idfasha1:" + idfasha1
    }
    val openudidsha1 = row.getAs[String]("openudidsha1")
    if (!StringUtils.isBlank(openudidsha1)) {
      driversId += "openudidsha1:" + openudidsha1
    }
    val androididsha1 = row.getAs[String]("androididsha1")
    if (!StringUtils.isBlank(androididsha1)) {
      driversId += "androididsha1:" + androididsha1
    }
    driversId.toArray
  }

  private val parseDeviceName:PartialFunction[Int, String] = {
    case 1 => "手机"
    case 2 => "平板"
    case _ => "未知"
  }

  private val parseClientName:PartialFunction[Int, String] = {
    case 1 => "android"
    case 2 => "ios"
    case 3 => "wp"
    case _ => "未知"
  }

}
